<?php

namespace App\Admin\Controllers;

use App\Models\DevBookInfo;
use App\Models\DevUniversityMajor;
use App\Models\Goods;
use App\Models\Major;
use App\Models\UniversityBook;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;

class ExampleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {

       /* //导入专业
        DB::transaction(function () {
            $data = DB::table('dev_university_major')->pluck('zhuanye')->toArray();
            foreach ($data as $v) {
                $tmp['university_id'] = 1;
                $tmp['name'] = $v;
                Major::create($tmp);
            }
        });*/

       /* //导入书籍
        DB::transaction(function () {
            $data = DevBookInfo::select('id', 'jiaocai as name', 'chubanse as press', 'author')->get()->toArray();
            foreach ($data as $v) {
                $oldId = $v['id'];
                unset($v['id']);
                $newId = DB::table('goods')->insertGetId($v);
                DB::table('dev_book_info')->where('id', $oldId)->update(['new_id' => $newId]);
            }
        });*/

       /*//按学期导入书籍到学校书库
        DB::transaction(function () {
            $semeter=7;//学期
            $filed='class_'.$semeter;
            $data=DevUniversityMajor::where([[$filed,'<>',''],[$filed,'<>',0]])->select('id as major_id',$filed)->get()->toArray();
            foreach($data as $v){
                $temp=explode(',',$v[$filed]);
                foreach($temp as $v1){
                    $temp1['major_id']=$v['major_id'];
                    $goods=DevBookInfo::find($v1);
                    if(!$goods){
                        echo $v1;die;
                    }
                    $temp1['goods_id']=$goods->new_id;
                    $temp1['course']=$goods->classname;
                    $temp1['university_id']=1;
                    $temp1['semester']=$semeter;
                    UniversityBook::create($temp1);
                }
            }
        });*/

    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(YourModel::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(YourModel::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
